package com.foreknow.elm.controller;

import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.service.DeliveryaddressService;
import com.foreknow.elm.service.impl.DeliveryaddressServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class DeliveryaddressController {
    /**
     * 根据用户编号查询所属送货地址
     * @param userId
     * @return
     */
    public Object listDeliveryAddressByUserId(HttpServletRequest request){
        String ro =request.getParameter("userId");
        String  userId=String.valueOf(ro);
        DeliveryaddressService deliveryaddressService = new DeliveryaddressServiceImpl();
        List<Deliveryaddress> list = deliveryaddressService.listDeliveryAddressByUserId(userId);
        return list;
    }
    /**
     * 根据送货地址编号查询送货地址
     * @param daId
     * @return
     */
    public Object getDeliveryAddressById(HttpServletRequest request){
        String ro =request.getParameter("daId");
        Integer daId =Integer.valueOf(ro);
        DeliveryaddressService deliveryaddressService = new DeliveryaddressServiceImpl();
        Deliveryaddress deliveryaddress= deliveryaddressService.getDeliveryAddressById(daId);
        return deliveryaddress;
    }

    public Object saveDeliveryAddress(HttpServletRequest request){
        Deliveryaddress deliveryaddress = new Deliveryaddress();
        deliveryaddress.setContactName(request.getParameter("contactName"));
        deliveryaddress.setContactSex(Integer.valueOf(request.getParameter("contactSex")));
        deliveryaddress.setContactTel(request.getParameter("contactTel"));
        deliveryaddress.setAddress(request.getParameter("address"));
        deliveryaddress.setUserId(request.getParameter("userId"));
        DeliveryaddressService deliveryaddressService = new DeliveryaddressServiceImpl();
        int result=deliveryaddressService.saveDeliveryAddress(deliveryaddress);
        return result;
    }

    public Object updateDeliveryAddress(HttpServletRequest request){
        Deliveryaddress deliveryaddress = new Deliveryaddress();
        deliveryaddress.setContactName(request.getParameter("contactName"));
        deliveryaddress.setContactSex(Integer.valueOf(request.getParameter("contactSex")));
        deliveryaddress.setContactTel(request.getParameter("contactTel"));
        deliveryaddress.setAddress(request.getParameter("address"));
        deliveryaddress.setUserId(request.getParameter("userId"));
        deliveryaddress.setDaId(Integer.valueOf(request.getParameter("daId")));
        DeliveryaddressService service = new DeliveryaddressServiceImpl();
        int result = service.updateDeliveryAddress(deliveryaddress);
        return result;
    }

    public Object removeDeliveryAddress(HttpServletRequest request){
        String rs =request.getParameter("daId");
        Integer daId =Integer.valueOf(rs);
        DeliveryaddressService deliveryaddressService = new DeliveryaddressServiceImpl();
        int result= deliveryaddressService.removeDeliveryAddress(daId);
        return result;
    }
}
